using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.TestDrivens;
using Pixysoft.Framework.Configurations.Core;

namespace Pixysoft.Framework.Configurations.TestCases
{
    [TestDriven(TestDrivenType.IntegrationTest, "XmlNode")]
    class UnitTest_013_XmlNode
    {
        static IAssert assert = Assert.New(true);

        [TestStart]
        public static bool TestCaseStart()
        {
            return true;
        }

        [TestStop]
        public static void TestCaseStop()
        {
        }

        [TestStateSave]
        public static ITestDrivenDocument TestCaseStateSave()
        {
            return assert.Document;
        }

        [TestCase("Serialize")]
        public static void TestCase_000_Serialize()
        {
            string elementname = "config";
            string text = "helloworld";

            XmlNode node = new XmlNode(elementname);
            node.Text = text;
            IXmlProperty property = node.Properties.Add("hello", "hello");

            node.Node.Add(node);
            node.Nodes.Add(node);

            assert.Write(node.Text);
            assert.Write(node.ElementName);

            foreach (IXmlProperty subproperty in node.Properties)
            {
                assert.Write(subproperty.Key);
                assert.Write(subproperty.Value);
            }

            assert.IsEqual(property, node.Properties[0]);
            assert.IsEqual("hello", node.Properties["hello"]);
            assert.IsEqual("hello", node.Properties.Get<string>("hello"));
            assert.IsTrue(node.Properties.Contains("hello"));
            assert.IsEqual(1, node.Properties.Count);
            node.Properties.Clear();
            assert.IsEqual(0, node.Properties.Count);

            foreach (IXmlNode subnode in node.Node)
            {
                assert.Write(subnode.Text);
                assert.Write(subnode.ElementName);
            }
            assert.IsEqual(node, node.Node[0]);
            assert.IsEqual(node, node.Node[elementname]);
            assert.IsTrue(node.Node.Contains(elementname));
            assert.IsEqual(2, node.Node.Count);




            foreach (IXmlNode subnode in node.Nodes)
            {
                assert.Write(subnode.Text);
                assert.Write(subnode.ElementName);
            }
            assert.IsEqual(node, node.Nodes[0]);
            assert.IsEqual(2, node.Nodes.Count);
            assert.IsTrue(node.Nodes.Contains(elementname));
            assert.IsEqual(2, node.Nodes.Count);

            foreach (IXmlNode subnode in node.Nodes[elementname])
            {
                assert.Write(subnode.Text);
                assert.Write(subnode.ElementName);
            }
            assert.IsEqual(node, node.Nodes[elementname][0]);
            assert.IsEqual(2, node.Nodes[elementname].Count);
            assert.IsTrue(node.Nodes[elementname].Contains(elementname));
            assert.IsEqual(2, node.Nodes[elementname].Count);


            assert.Write("remove node from nestcollection");


        }

        [TestCase("Serialize")]
        public static void TestCase_001_Serialize()
        {
            string elementname = "config";
            string text = "helloworld";

            XmlNode node = new XmlNode(elementname);
            node.Text = text;
            IXmlProperty property = node.Properties.Add("hello", "hello");

            node.Node.Add(node);
            node.Nodes.Add(node);
            node.Nodes[elementname].Add("element2", "element2");

            assert.Write("remove node from nestcollection");

            assert.IsEqual(3, node.Nodes.Count);

            IXmlNodesCollection nodes = node.Nodes[elementname][elementname].Remove(elementname);

            assert.IsEqual(1, node.Nodes.Count);

            node.Nodes[elementname].Add("element2", "element2");

            assert.IsEqual(2, node.Nodes.Count);

            assert.IsFalse(node.Nodes[elementname].Contains("element2"));

            assert.IsFalse(node.Nodes[elementname].Contains(elementname));
        }

        [TestCase("Serialize")]
        public static void TestCase_002_Serialize()
        {
            string elementname = "config";
            XmlNode node = new XmlNode(elementname);
            IXmlProperty property = node.Properties.Add("hello", "hello");

            IXmlNode subnode = node.Node.Add("node1", "node1");
            subnode.Properties.Add("world", "world");
            node.Node.Add("node2", "node2");

            assert.Write(node.Serialize());

            assert.Write(node.Serialize(true));

            Assert.Cache.Push(node);
        }
    }
}
